import {getApplyOrgs} from '@/services';
import {Toast} from 'antd-mobile'


export default {
    namespace: 'apply',
    state: {
        // 表单数据
        base: {
            name: '',
            applyPsn: "",
            applyOrgCode: [],
            applyDept: [],
            cause: "",
            feeType: "1",
            feeOrg: "",
            feeOrgCode: "",
            feeOrgName: "",
            project: "",
            projectNo: "",
            projectName: "",
        },
        // 行程明细
        trips: [],
        // 申请组织
        applyOrgs: [],
        applyDepts: [],
        // 是否显示申请组织面板
        isApplyOrgPanelShow: false,
    },
    reducers: {
        handleFormValuesChange(state, {payload: {changedValues, allValues}}) {
            const base = {...state.base, ...changedValues}
            return {...state, base};
        },
        setApplyOrgs(state, {payload: {applyOrgs = []}}) {
            state.applyOrgs = applyOrgs;
            // return state;
            // return {...state, {applyOrgs: applyOrgs1}};
        },
        setUser(state) {
            const username = sessionStorage.getItem("username");
            const usercode = sessionStorage.getItem("usercode");
            const deptsStr = sessionStorage.getItem("depts");
            if (null == deptsStr) {
                // todo
                state.base.name = "username";
                state.base.applyPsn = "usercode";
                state.applyDepts = [
                    {
                        label: "测试",
                        value: 1
                    }
                ];
                return;
            }
            const depts = JSON.parse(deptsStr);
            const deptsWithLabel = depts.map(item => ({
                label: item.name,
                value: item.id
            }))
            state.base.name = username;
            state.base.applyPsn = usercode;
            state.applyDepts = deptsWithLabel;
        },
        setProjectFromLocationState(state, {payload: {id, name, code}}) {
            state.base.project = id;
            state.base.projectNo = code;
            state.base.projectName = name;
        },
        setFeeOrgFromLocationState(state, {payload: {id, name, code}}) {
            state.base.feeOrg = id;
            state.base.feeOrgCode = code;
            state.base.feeOrgName = name;
        },

    },
    effects: {
        * handleGetApplyOrgs(payload: any, {put, call}: any) {
            const response = yield call(getApplyOrgs)
            const {code, message, data} = response;
            if (code === "0") {
                // 成功
                const orgs = data.map(item => ({
                    label: item.name,
                    value: item.id,
                }));
                yield put({
                    type: "setApplyOrgs",
                    payload: {
                        applyOrgs: orgs
                    }
                })
            } else {
                Toast.show({
                    icon: 'fail',
                    content: message,
                })
            }

        },
    },
    subscriptions: {
        setupApplyOrgs({dispatch}) {
            dispatch({
                type: 'handleGetApplyOrgs'
            });
        },
        setupUser({dispatch}) {
            dispatch({
                type: 'setUser'
            });
        },
        setuph ({dispatch, history}) {
            history.listen(( pathname ) => {
                console.log("|||||")
                console.log(pathname)
            })
        }
    },
};
